package icu.makka.yygh.common.exception;

import icu.makka.yygh.result.Result;
import icu.makka.yygh.result.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理器
 */
@Slf4j
@ControllerAdvice(basePackages = "icu.makka")
@ResponseBody
public class GlobalExceptionHandler{

    @ExceptionHandler(CustomException.class)
    public Result customException(CustomException ex) {
        log.info("全局异常处理器捕捉到自定义异常，message={}", ex.getMessage());
        return Result.fail(ex.getCode(), ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result error(Exception ex) {
        log.info("全局异常处理器捕捉到异常，message={}", ex.getMessage());
        return Result.fail(ex.getMessage());
    }
}
